#include <REGX52.H>
#include <INTRINS.H>
#include "delay.h"

//sfr
//special function register
//special bit 
sbit SER 	= P3^4;		//SER
sbit RCK	= P3^5;		//RCLK
sbit SRCLK 	= P3^6;		//SRCLK


#define MATRIX_LED_PORT P0

/**
  * @brief  74HC595写入一个字节
  * @param  Byte 要写入的字节
  * @retval 无
  */
void _74HC595_WriterByte(unsigned char Byte)
{
	
	SER = Byte & 0x80;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	SER = Byte & 0x40;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	SER = Byte & 0x20;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	SER = Byte & 0x10;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	
	SER = Byte & 0x08;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	SER = Byte & 0x04;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	SER = Byte & 0x02;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	SER = Byte & 0x01;		//非0即1
	SRCLK = 1;
	SRCLK = 0;
	
	
	//移位寄存器并行输出到锁存寄存器
	RCK = 1;
	RCK = 0;
	
}


void _74HC595_Init()
{
	//单片机启动默认IO口为高电平，需要初始化RCLK, SRCLK为低电平
	SRCLK = 0;
	RCK = 0;
}

//最左边列为低0列，最右边列为第7列
/**
  * @brief  LED点阵屏显示一列数据
  * @param  Column 要显示的列，范围0~7，0在最左边
  * @param  Data 选择列显示的数据，高位在上，1为亮，0为灭
  * @retval 无
  */
void _74HC595_ShowColumn(unsigned char Column, unsigned char Data)
{
	
	_74HC595_WriterByte(Data);
	MATRIX_LED_PORT = ~(0x80 >> Column);
	
	//同数码管一样，LED点阵也需要消影
	Delay(1); 		//延时1ms给数据显示时间，延时时间太短的话LED亮度不够
	MATRIX_LED_PORT = 0xFF;		//清除位选
	
}


void main()
{
	unsigned char i = 0;
	
	P0 = 0xFF;
	
	_74HC595_Init();
	_74HC595_WriterByte(0xFF);
	
	
	
	
	while(1)
	{
//		P0 = ~(1 << i);
//		Delay(200);
//		
//		i++;
//		if(i >= 8)
//			i = 0;
		
		_74HC595_ShowColumn(0, 0x3C);
		_74HC595_ShowColumn(1, 0x42);
		_74HC595_ShowColumn(2, 0x99);
		_74HC595_ShowColumn(3, 0x85);
		_74HC595_ShowColumn(4, 0x85);
		_74HC595_ShowColumn(5, 0x99);
		_74HC595_ShowColumn(6, 0x42);
		_74HC595_ShowColumn(7, 0x3C);
		
		
	}
}
